//
//  OperateListViewController.m
//  ShrimpCrowdordering
//
//  Created by 郑洲 on 2018/7/27.
//  Copyright © 2018年 郑洲. All rights reserved.
//

#import "OperateListViewController.h"
#import "UIImage+GradientColor.h"
#import "OperateHistoryCell.h"

@interface OperateListViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) BaseTableView *tableView;

@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, assign) NSInteger currentPage;
@property (nonatomic, assign) NSInteger totalPage;
@property (nonatomic, strong) UILabel *totalNum;
@property (nonatomic, strong) UIImageView *selectImg;
@property (nonatomic, strong) UIButton *selectBtn;
@property (nonatomic, assign) NSInteger type;

@end

@implementation OperateListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"提现记录";
    
    _type = 1;
    
    self.dataArray = [NSMutableArray array];
    self.currentPage = 1;
    
    _totalNum =[[UILabel alloc] initWithFrame:CGRectMake(ScreenX375(0), ScreenX375(0), ScreenX375(65), ScreenX375(27))];
    _totalNum.text = @"共26条";
    _totalNum.font = Kfont(14);
    _totalNum.textColor = REXADECIMALCOLOR(@"#f21934");
    _totalNum.textAlignment = NSTextAlignmentCenter;
    _totalNum.layer.borderWidth = 1;
    _totalNum.layer.cornerRadius = 13.5;
    _totalNum.layer.borderColor = REXADECIMALCOLOR(@"#f21934").CGColor;
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:_totalNum];
    
    self.tableView = [[BaseTableView alloc] initWithFrame:CGRectMake(0, ScreenX375(39), SCREEN_WIDTH, SCREEN_HEIGHT - NAVIGATION_BAR_HEIGHT - ScreenX375(39))];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.rowHeight = ScreenX375(60);
    [self.view addSubview:self.tableView];
    
    NSMutableArray *arr = [NSMutableArray array];
    for (int i = 0; i < 12; i++) {
        NSString *imageName = [NSString stringWithFormat:@"%d",i];
        [arr addObject:ImageWithName(imageName)];
    }
    MJRefreshGifHeader *header = [MJRefreshGifHeader headerWithRefreshingTarget:self refreshingAction:@selector(headerRefresh)];
    [header setImages:arr forState:MJRefreshStateRefreshing];
    self.tableView.mj_header = header;
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerRefresh)];
    [self.tableView.mj_header beginRefreshing];
    
    [self setSelectView];
}

- (void)setSelectView {
    UIView *selectView = [[UIView alloc] initWithFrame:CGRectMake(ScreenX375(80), ScreenX375(8), ScreenX375(215), ScreenX375(31))];
    [self.view addSubview:selectView];
    
    UIView *titleBg = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenX375(215), ScreenX375(31))];
    titleBg.layer.cornerRadius = ScreenX375(15.5);
    titleBg.layer.borderWidth = 0.5;
    titleBg.layer.borderColor = REXADECIMALCOLOR(@"#eaeaea").CGColor;
    titleBg.layer.masksToBounds = YES;
    [selectView addSubview:titleBg];
    
    UIImage *bgImg = [UIImage gradientColorImageFromColors:@[REXADECIMALCOLOR(@"#fd4d4d"),REXADECIMALCOLOR(@"#ff1062")] gradientType:GradientTypeLeftToRight imgSize:CGSizeMake(ScreenX375(120), ScreenX375(31))];
    _selectImg = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, ScreenX375(120), ScreenX375(31))];
    _selectImg.image = bgImg;
    _selectImg.layer.cornerRadius = ScreenX375(15.5);
    _selectImg.clipsToBounds = YES;
    [selectView addSubview:_selectImg];
    
    NSArray *noteArr = @[@"余额记录",@"虾币记录"];
    for (int i = 0; i < 2; i++) {
        UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(i * ScreenX375(95), 0, ScreenX375(120), ScreenX375(31))];
        [button setTitle:noteArr[i] forState:UIControlStateNormal];
        [button setTitleColor:REXADECIMALCOLOR(@"#ffffff") forState:UIControlStateSelected];
        [button setTitleColor:REXADECIMALCOLOR(@"#888888") forState:UIControlStateNormal];
        button.titleLabel.font = Kfont(15);
        if (i == 0) {
            button.selected = YES;
            _selectBtn = button;
        }
        button.tag = 100 + i;
        [button addTarget:self action:@selector(selectType:) forControlEvents:UIControlEventTouchUpInside];
        [selectView addSubview:button];
    }
}

- (void)headerRefresh {
    _currentPage = 1;
    [self getData];
    self.tableView.mj_footer.hidden = NO;
}

- (void)footerRefresh {
    if (_currentPage <= _totalPage) {
        
        _currentPage++;
        [self getData];
        // 结束刷新
        [self.tableView.mj_footer endRefreshing];
    }else{
        
//        [HttpManager showNoteMsg:@"已经到底啦"];
        // 结束刷新
        [self.tableView.mj_footer endRefreshing];
        self.tableView.mj_footer.hidden = YES;
    }
}

- (void)selectType:(UIButton *)button {
    button.selected = YES;
    _selectBtn.selected = NO;
    _selectBtn = button;
    if (button.tag == 100) {
        _type = 1;
        [UIView animateWithDuration:0.3 animations:^{
            self.selectImg.frame = CGRectMake(0, 0, ScreenX375(120), ScreenX375(31));
        }];
    }else {
        _type = 0;
        [UIView animateWithDuration:0.3 animations:^{
            self.selectImg.frame = CGRectMake(ScreenX375(95), 0, ScreenX375(120), ScreenX375(31));
        }];
    }
    [_tableView.mj_header beginRefreshing];
}

- (void)getData {
    NSMutableDictionary *parameter = [NSMutableDictionary dictionaryWithDictionary:@{@"userId":kNSUDefaultReadKey(@"userId"),@"pageSize":@"10",@"page":[NSNumber numberWithInteger:_currentPage],@"status":[NSNumber numberWithInteger:_type]}];
    [HttpManager postHttpRequestByGet:[NSString stringWithFormat:@"%@api/getWealthRecordList",REQUESTHEADER] andParameter:parameter success:^(id successResponse) {
        NSString *status = [NSString stringWithFormat:@"%@",successResponse[@"code"]];
        if ([status isEqualToString:@"200"]) {
            if (self.currentPage == 1) {
                [self.dataArray removeAllObjects];
            }
            self.totalPage = [successResponse[@"data"][@"pages"] integerValue];
            self.totalNum.text = [NSString stringWithFormat:@"共%@条",successResponse[@"data"][@"total"]];
            
            NSError *error = nil;
            for (NSDictionary *dic in successResponse[@"data"][@"list"]) {
                OperateModel *model = [[OperateModel alloc] initWithDictionary:dic error:&error];
                [self.dataArray addObject:model];
            }
            if (self.totalPage == self.currentPage) {
                self.tableView.mj_footer.hidden = YES;
            }
            if ([successResponse[@"data"][@"list"] count] == 0) {
                [HttpManager showNoteMsg:@"暂无更多数据"];
                self.tableView.mj_footer.hidden = YES;
            }
            [self.tableView reloadData];
        }else {
            [HttpManager showNoteMsg:[NSString stringWithFormat:@"%@",successResponse[@"msg"]]];
        }
        if ([self.tableView.mj_header isRefreshing]) {
            [self.tableView.mj_header endRefreshing];
        }else if ([self.tableView.mj_footer isRefreshing]) {
            [self.tableView.mj_footer endRefreshing];
        }
    } andFailure:^(id failureResponse) {
        [HttpManager showFail];
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    OperateHistoryCell *cell = [OperateHistoryCell cellWithTableView:tableView andIndexPath:indexPath];
    cell.operateModel = _dataArray[indexPath.row];
    return cell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
